package von.seiji.cn.test;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.junit.Test;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Author: SeiJi
 * @DateTime: 2022/4/17 4:50
 * @Description: 此人很懒，未写描述！
 **/
public class T14 {

    @Test
    public void dklsf9() {
        URL url = ClassLoader.getSystemResource("data/live2d.png");
        BufferedImage read = ImgUtil.read(url);
        int height = read.getHeight();
        int width = read.getWidth();

        List<ArrayList<Integer>> collect = IntStream.range(0, height).mapToObj(
                y -> IntStream.range(0, width).mapToObj(
//                        x -> Lists.newArrayList(x, y)
                        x -> {
                            int rgb = read.getRGB(x, y);
                            if (0xff000000 != rgb && 0 != rgb) {
                                return Lists.newArrayList(x, y);
                            }
                            return null;
                        }
                ).collect(Collectors.toList())
        ).flatMap(List::stream).filter(Objects::nonNull).collect(Collectors.toList());

        Map<String, List<ArrayList<Integer>>> collect2 = collect.stream().collect(Collectors.groupingBy(
                item -> "#" + StrUtil.fill(Integer.toHexString(0x00ffffff & read.getRGB(item.get(0), item.get(1))), '0', 6, true)
        ));

        Set<String> color2 = collect2.keySet();
        System.out.println(color2);

 /*       List<Map<String, Object>> collect1 = collect2.entrySet().stream().map(item -> {
            Map<String, Object> map = Maps.newHashMap();
            map.put("type", "scatter");
            map.put("symbolSize", 2);
            item.getValue().forEach(px -> {
                px.add(1, height - px.get(1) -1);
                px.remove(2);
            });
            map.put("data", item.getValue());
            return map;
        }).collect(Collectors.toList());*/

        List<String> color3 = collect.stream().map(item ->
                "#" + StrUtil.fill(Integer.toHexString(0x00ffffff & read.getRGB(item.get(0), item.get(1))), '0', 6, true)
        ).collect(Collectors.toList());

        Map<String, Object> map = Maps.newHashMap();
        map.put("type", "scatter");
        map.put("symbolSize", 2);
        collect.forEach(px -> {
            px.add(1, height - px.get(1) -1);
            px.remove(2);
        });
        map.put("data", collect);

        System.out.println("map = " + map);



//        System.out.println(collect1);

    }

    @Test
    public void dkls9() {
        URL url = ClassLoader.getSystemResource("data/option.json");
        String string = FileUtil.readUtf8String(url.getPath());
        System.out.println("string = " + string);
        FileUtil.writeString(JSON.toJSONString(JSON.parse(string))
        , new File("./beauty.json"), CharsetUtil.UTF_8);
    }

    @Test
    public void dks() {
        URL url = ClassLoader.getSystemResource("rainbow.json");
        String string = FileUtil.readUtf8String(url.getPath());
        JSONObject object = JSON.parseObject(string);
        object.put("x",
                IntStream.generate(() ->
                        RandomUtil.randomInt(300)
                ).limit(260)
                        .mapToObj(String::valueOf).collect(Collectors.toList())
        ); object.put("y",
                IntStream.generate(() ->
                        RandomUtil.randomInt(360)
                ).limit(260)
                        .mapToObj(String::valueOf).collect(Collectors.toList())
        );
        JSONObject marker = object.getJSONObject("marker");
        List<Integer> collect = IntStream.generate(() -> RandomUtil.randomInt(4111))
                .limit(260).boxed().collect(Collectors.toList());
        JSONArray color = marker.getJSONArray("color");
        marker.put("color",
                collect.stream().map(item -> color.getString(item))
                .collect(Collectors.toList())
        );

        JSONArray size = marker.getJSONArray("size");
        marker.put("size",
                collect.stream().map(item -> size.getString(item))
                        .collect(Collectors.toList())
        );

        System.out.println("object = " + object);


    }

}
